#!/usr/bin/python2.4


class InspectorBase(object):

  def __init__(self, test_results):
    self._test_results = test_results

  @staticmethod
  def groupby_nonzero(function, sequence):
    if not function:
      return sequence, []
    true_list = []
    false_list = []
    for element in sequence:
      if function(element):
        true_list.append(element)
      else:
        false_list.append(element)
    return true_list, false_list

  def filter_results(self, function_name, sequence):
    pre_function = getattr(self, 'pre_%s' % function_name, None)
    if pre_function:
      pre_function()
    function = getattr(self, function_name, None)
    results = self.groupby_nonzero(function, sequence)
    post_function = getattr(self, 'post_%s' % function_name, None)
    if post_function:
      post_function()
    return results

  def filter_test_views(self, test_views):
    return self.filter_results('inspect_test_view', test_views)

  def filter_job_host_runs(self, job_host_runs):
    return self.filter_results('inspect_job_host_run', job_host_runs)

  def filter_hosts(self, hosts):
    return self.filter_results('inspect_host', hosts)
